package org.webball.pitch.phase;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.webball.base.abstracts.AbstractBall;
import org.webball.base.abstracts.AbstractPlayer;
import org.webball.pitch.Pitch;
import org.webball.pitch.PitchConstants;
import org.webball.pitch.Team;
import org.webball.tools.MKVector;

/**
 * 
 * @author Maciej Koch
 *
 */
public class TacticalPhase extends BasicPhase {

	//time
	private int time; 
	private int timer;
	private int timeToEnd;

	public TacticalPhase(Team[] teams, AbstractBall ball, Pitch pitch) {
		super(teams, ball, pitch);
		timer = 0;
		time = new GregorianCalendar().get(Calendar.SECOND);
	
		//all players position & movement target
		setPositionAndTarget();
		
		//set team serving
		setServing(attackingTeam);		
			
	}
	@Override
	public void update() {
		//time to end this phase
		calculateTime();
	}
	public int getTimeToEnd() {
		return timeToEnd;
	}
	//------------------------------
	//---------private--------------
	//------------------------------
	@Override
	protected void stop() {
		pitch.setPhase(Phases.PLAY);	
	}
	
	private void calculateTime() {
		int now = new GregorianCalendar().get(Calendar.SECOND);
		if(now != time) {
			timer++;
			time++;
			if(time == 60) time = 0;
		}		
		timeToEnd = PitchConstants.tacticalPhaseTime - timer;
		
		if(timeToEnd == 0) stop();
	}	
	private void setPositionAndTarget() {
		//all players position & movement target
		//team 0
		for(int i = 0 ; i < 6 ; i++) {
			AbstractPlayer player = teams[Team.T1].getPlayer(i);
			switch(i) {
			case 0:
				player.setPosition(new MKVector(250,50).add(PitchConstants.firstStart));
				break;
			case 1:
				player.setPosition(new MKVector(250,150).add(PitchConstants.firstStart));
				break;
			case 2:
				player.setPosition(new MKVector(250,250).add(PitchConstants.firstStart));
				break;	
			case 3:
				player.setPosition(new MKVector(50,50).add(PitchConstants.firstStart));
				break;	
			case 4:
				player.setPosition(new MKVector(100,150).add(PitchConstants.firstStart));
				break;	
				//serving
			case 5:
				player.setPosition(new MKVector(50,250).add(PitchConstants.firstStart));
				break;	
			}
		}
		//team 1
		for(int i = 0 ; i < 6 ; i++) {
			AbstractPlayer player = teams[Team.T2].getPlayer(i);
			switch(i) {
			case 0:
				player.setPosition(new MKVector(50,250).add(PitchConstants.secondStart));
				break;
			case 1:
				player.setPosition(new MKVector(50,150).add(PitchConstants.secondStart));
				break;
			case 2:
				player.setPosition(new MKVector(50,50).add(PitchConstants.secondStart));
				break;	
			case 3:
				player.setPosition(new MKVector(250,250).add(PitchConstants.secondStart));
				break;	
			case 4:
				player.setPosition(new MKVector(200,150).add(PitchConstants.secondStart));
				break;	
				//serving
			case 5:
				player.setPosition(new MKVector(250,50).add(PitchConstants.secondStart));
				break;	
			}
		}
	}
	private void setServing(int teamId) {
		//serving
		AbstractPlayer player = teams[teamId].getPlayer(5);
		switch(teamId) {
		case Team.T1:
			player.setPosition(PitchConstants.firstServePoint);		
			player.setHitTarget(new MKVector(150,150).add(PitchConstants.secondStart));
			player.setMovementTarget(new MKVector(50,250).add(PitchConstants.firstStart));
			break;
		case Team.T2:
			player.setPosition(PitchConstants.secondServePoint);
			player.setHitTarget(new MKVector(150,150).add(PitchConstants.firstStart));
			player.setMovementTarget(new MKVector(250,50).add(PitchConstants.secondStart));
			break;	
		}
		ball.setPosition(new MKVector(getServingPosition(teamId)));
	}
	private MKVector getServingPosition(int teamId) {
		return teams[teamId].getPlayer(5).getPosition();
	}
}
